//////////////////////////////////////////////////////////////////////
////                                                              ////
////  GPIORaw.v                                                   ////
////                                                              ////
////                                                              ////
////  This file is part of the "Pico E12" project                 ////
////  http://www.picocomputing.com                                ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
////                                                              ////
//// Copyright (C) 2005, Picocomputing, Inc.                      ////
//// http://www.picocomputing.com/                                ////
////                                                              ////
//// This source file may be used and distributed without         ////
//// restriction provided that this copyright statement is not    ////
//// removed from the file and that any derivative work contains  ////
//// the original copyright notice and the associated disclaimer. ////
////                                                              ////
//// This source file is free software; you can redistribute it   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// Public License as published by the Free Software Foundation; ////
//// either version 2.1 of the License, or (at your option) any   ////
//// later version.                                               ////
////                                                              ////
//// This source is distributed in the hope that it will be       ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// details.                                                     ////
////                                                              ////
//// You should have received a copy of the GNU Lesser General    ////
//// Public License along with this source; if not, download it   ////
//// from http://www.opencores.org/lgpl.shtml                     ////
////                                                              ////
////////////////////////////////////////////////////////////////////JF

`timescale 10ns / 1ns
`include "PicoDefines.v"

module GPIORaw(Enable, OutputEnable, MemRead, MemWrite, DataIn, DataOut, 
               GPIO_IN, GPIO_OUT, GPIO_TRIS);

input Enable;
input OutputEnable;
input MemRead;
input MemWrite;
input [15:0]DataIn;
output [15:0]DataOut;

input [4:1]GPIO_IN;
output [4:1]GPIO_OUT;
output [4:1]GPIO_TRIS;

reg [4:1]GPIO_OUT_REG;
reg [4:1]GPIO_TRIS_REG;
reg [4:1]GPIODataInLatch;

wire [4:1]GPIO_OUT;
wire [4:1]GPIO_TRIS;

//synthesis attribute INIT of GPIO_OUT is 0;
//synthesis attribute INIT of GPIO_TRIS is TRUE;


wire MemReadBUF;
                                                             
BUF MemReadBuffer(.I(MemRead), .O(MemReadBUF));                        //Buffer the global memory read and write signals off the clock busses

wire [15:0]RawDataOut;

assign RawDataOut[3:0] = GPIO_OUT[4:1];
assign RawDataOut[7:4] = GPIODataInLatch[4:1];
assign RawDataOut[8] = ~GPIO_TRIS[1];
assign RawDataOut[9] = ~GPIO_TRIS[2];
assign RawDataOut[10] = ~GPIO_TRIS[3];
assign RawDataOut[11] = ~GPIO_TRIS[4];
assign RawDataOut[15:12] = 4'b0;

assign DataOut[15:0] = (Enable && MemReadBUF)? RawDataOut[15:0] : 16'b0;
assign GPIO_OUT[4:1] = (OutputEnable) ? GPIO_OUT_REG[4:1] : 4'h0;
assign GPIO_TRIS[4:1] = (OutputEnable) ? GPIO_TRIS_REG[4:1] : 4'hF;

always @(posedge MemRead)
   if (Enable) GPIODataInLatch[4:1] <= GPIO_IN[4:1];

always @(posedge MemWrite)
   if (Enable) begin
      GPIO_OUT_REG[4:1] <= DataIn[3:0];
	   GPIO_TRIS_REG[1] <= ~DataIn[8];
	   GPIO_TRIS_REG[2] <= ~DataIn[9];
	   GPIO_TRIS_REG[3] <= ~DataIn[10];
	   GPIO_TRIS_REG[4] <= ~DataIn[11];
   end

endmodule